#include "toast.h"

#include <QApplication>
#include <QScreen>
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>

Toast::Toast(const QString& text)
{
    // 1. 设置窗口本身属性
    this->setFixedSize(500, 100);
    this->setWindowTitle("新消息通知");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(243, 243, 243); }");
    this->setAttribute(Qt::WA_DeleteOnClose);
    // this->setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint);
    // 去掉标题栏
    // this->setWindowFlags(Qt::FramelessWindowHint);

    // 2. 获取屏幕的尺寸
    QScreen* screen = QApplication::primaryScreen();
    int width = screen->size().width();
    int height = screen->size().height();
    int x = (width - this->width()) / 2;
    int y = height - this->height() - 100;
    this->move(x, y);

    // 2. 布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(layout);


    // 4. 显示文本的label
    QLabel* label = new QLabel();
    label->setStyleSheet("QLabel { font-weight: 450; font-size: 18px; color: rgb(127, 127, 127); }");
    label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label->setText(text);
    label->setAlignment(Qt::AlignCenter);
    layout->addWidget(label);

    // 5. 实现 2s 之后自动关闭
    QTimer* timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [=](){
        timer->stop();
        this->close();
    });
    timer->start(2000);
}

void Toast::showMessage(const QString &text)
{
    Toast* toast = new Toast(text);
    toast->show();
}











